﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <copyright>Copyright 2008-2011 Andreas Huber Doenni</copyright>
// Distributed under the Microsoft Public License (Ms-PL).
// See accompanying file License.txt or copy at http://mews.codeplex.com/license.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Mews.Application
{
    using System.Drawing;

    /// <summary>Provides helper methods for icons.</summary>
    internal static class IconHelper
    {
        internal static Bitmap ConvertToBitmap(Icon icon, Color backgroundColor)
        {
            using (var bitmapGuard = ScopeGuard.Create(new Bitmap(icon.Width, icon.Height)))
            using (var graphics = Graphics.FromImage(bitmapGuard.Resource))
            using (var brush = new SolidBrush(backgroundColor))
            {
                // Apparently, drawing with the alpha channel is not supported on the compact framework, which is
                // why we have to fill the background color first.
                // http://msdn.microsoft.com/en-us/library/ms172503.aspx
                graphics.FillRectangle(brush, 0, 0, bitmapGuard.Resource.Width, bitmapGuard.Resource.Height);
                graphics.DrawIcon(icon, 0, 0);
                bitmapGuard.Dismiss();
                return bitmapGuard.Resource;
            }
        }
    }
}
